﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.BanLe
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            XuLy();
            dgvHienThi.DataSource = hienthi;
            fix();
            if (capnhat == true)
            {
                Them();
            }
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.HDBanLe hdbanle;
        Entities.HDBanLe[] hienthi;
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        string makho = "";
        string tenkho = "";
        int i = 0;

        public frmQuanLy()
        {
            InitializeComponent();
            try
            {
                date = Entitystatic.date;
                batdau = ketthuc = date;
                Entities.CaiDatKho kho = Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                XuLy();
                dgvHienThi.DataSource = hienthi;
                fix();
            }
            catch
            {
            }
        }

        void XuLy()
        {
            hienthi = (Entities.HDBanLe[])(Common.StaticVariable.InterfaceObject.SelectHDBanLe().Where(p => p.MaKho == makho).ToArray());
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TongTienThanhToan].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanLe.MaHDBanLe].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanLe.TenKhachHang].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanLe.TenKho].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanLe.NgayLap].Visible = true;
            dgvHienThi.Columns[Common.Constants.HDBanBuon.TongTienThanhToan].HeaderText = "Tổng Tiền";
            dgvHienThi.Columns[Common.Constants.HDBanLe.MaHDBanLe].HeaderText = "Mã Hóa Đơn";
            dgvHienThi.Columns[Common.Constants.HDBanLe.TenKhachHang].HeaderText = "Tên Khách Hàng";
            dgvHienThi.Columns[Common.Constants.HDBanLe.TenKho].HeaderText = "Tên Kho";
            dgvHienThi.Columns[Common.Constants.HDBanLe.NgayLap].HeaderText = "Ngày Lập";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Check Permission
        private Boolean CheckPerMission(int active)
        {
            return Common.Utilities.Check(Common.FormName.Quanlybanle, active);
        }
        #endregion

        #region Thêm
        public void Them()
        {
            try
            {
                if (CheckPerMission(1) != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }

                frmXuLy frm = new frmXuLy();
                frm.MyGetData = new frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region Sửa
        void Sua()
        {
            try
            {
                if (CheckPerMission(2) != true)
                {
                    MessageBox.Show("Bạn không có quyền xem", Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (i < 0)
                    return;
                if (dgvHienThi.RowCount == 0)
                    return;
                hdbanle = ((Entities.HDBanLe[])dgvHienThi.DataSource)[i];
                // check object
                if (hdbanle == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                frmXuLy frm = new frmXuLy(hdbanle);
                frm.MyGetData = new frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch
            {
            }
        }
        #endregion

        #region Lọc Điều Kiện
        void LocDieuKien()
        {
            try
            {
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy();
                    dgvHienThi.DataSource = hienthi;
                    fix();
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region In
        void In()
        {
            try
            {
                this.Enabled = false;

                if (i < 0)
                    return;
                if (dgvHienThi.RowCount == 0)
                    return;
                hdbanle = ((Entities.HDBanLe[])dgvHienThi.DataSource)[i];
                // check object
                if (hdbanle == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                Entities.ChiTietHDBanle[] cthdbb = new frmXuLy().HienThiChiTiet(hdbanle);
                int sotang = 0;
                double tongthuegtgt = 0;
                Entities.BCHDBanLe[] bc = new Entities.BCHDBanLe[cthdbb.Length];
                foreach (Entities.ChiTietHDBanle item in cthdbb)
                {
                    bc[sotang] = new Entities.BCHDBanLe();
                    bc[sotang].SoLuong = int.Parse(string.Format("{0:0}", double.Parse(item.SoLuong.ToString())));
                    bc[sotang].GiaBanle = double.Parse(item.DonGia);
                    bc[sotang].MaHDBanHang = item.MaHDBanLe;
                    bc[sotang].TenHangHoa = item.TenHangHoa;
                    double thue = double.Parse(item.ThueGTGT);
                    double dongia = double.Parse(item.DonGia);
                    double chietkhau = double.Parse(item.PhanTramChietKhau);
                    double dongiamoi = dongia - ((dongia * chietkhau) / 100);
                    int sl = int.Parse(string.Format("{0:0}", double.Parse(item.SoLuong.ToString())));
                    tongthuegtgt += sl * ((dongiamoi * thue)) / 100;
                    sotang++;
                }

                hdbanle.ThueGTGT = tongthuegtgt.ToString();
                BaoCao.frmBaoCaorpt frm = new BaoCao.frmBaoCaorpt(bc, hdbanle);
                if (frm.kiemtra)
                {
                    frm.Show();
                    frm.kiemtra = false;
                }
                this.Enabled = true;
            }
            catch
            {
            }
        }
        #endregion

        #region Thoát
        void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Tìm kiếm
        void TimKiem()
        {
            dgvHienThi.DataSource = (Entities.HDBanLe[])hienthi.Where(p => p.MaHDBanLe.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap; 
            this.FormClosing += new FormClosingEventHandler(frmQuanLy_FormClosing);
            btnIn.Click += new EventHandler(btnIn_Click);
            btnLocDieuKien.Click += new EventHandler(btnLocDieuKien_Click);
            btnSua.Click += new EventHandler(btnSua_Click);
            btnThem.Click += new EventHandler(btnThem_Click);
            btnThoat.Click += new EventHandler(btnThoat_Click);
            btnTim.Click += new EventHandler(btnTim_Click);
            txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
            dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
            dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
            dgvHienThi.KeyDown += new KeyEventHandler(dgvHienThi_KeyDown);
            this.WindowState = FormWindowState.Maximized;
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }

        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        void btnLocDieuKien_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        void btnIn_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }

        #endregion
    }
}
#endregion